﻿using System;
using System.Collections;
using System.Collections.Generic;
using CalcSharp.Core;
using CalcSharp.Core.Containers;
using CalcSharp.Core.Plugin;
using plug_mtrximage.HelperClasses;

namespace plug_mtrximage
{
    public class PlugMtrxColor : ICMDPlugin
    {
        private IPluginHost host;

        public Dictionary<string, string> autocomplete
        {
            get
            {
                Dictionary<string, string> elements = new Dictionary<string, string>();
                elements.Add("Matrix Color effects", "INVERT;BW;SEPIA;RGBBGR;CONTRAST;BRIGHTNESS;GAMMA;COLOR");
                return elements;
            }
        }

        public Tresult Run(ArrayList parameters)
        {
            Tresult result = new Tresult();
            string action = (parameters[1] as string).ToUpper();
            double controllval; //not required by all the plugins
            AMatrix inp, outp;
            inp = host.Mtrx_mem[(parameters[0] as string)];
            outp = inp;

            switch (action)
            {
                case "INVERT":
                    outp = Efects.SimpleColorEfect(inp, SimpleEfect.Invert, host.Mtrx_mem.StorageType);
                    break;
                case "BW":
                    outp = Efects.SimpleColorEfect(inp, SimpleEfect.Grayscale, host.Mtrx_mem.StorageType);
                    break;
                case "SEPIA":
                    outp = Efects.SimpleColorEfect(inp, SimpleEfect.Sepia, host.Mtrx_mem.StorageType);
                    break;
                case "RGB2BGR":
                    outp = Efects.SimpleColorEfect(inp, SimpleEfect.RGB2BGR, host.Mtrx_mem.StorageType);
                    break;
                case "CONTRAST":
                    controllval = Convert.ToDouble(parameters[2]);
                    outp = Efects.ContrastBrightness(inp, (int)controllval, ParamEfect.Contrast, host.Mtrx_mem.StorageType);
                    break;
                case "BRIGHTNESS":
                    controllval = Convert.ToDouble(parameters[2]);
                    outp = Efects.ContrastBrightness(inp, (int)controllval, ParamEfect.Brightness, host.Mtrx_mem.StorageType);
                    break;
                case "GAMMA":
                    controllval = Convert.ToDouble(parameters[2]);
                    outp = Efects.Gamma(inp, controllval, host.Mtrx_mem.StorageType);
                    break;
                case "COLOR":
                    double p2, p3;
                    controllval = Convert.ToDouble(parameters[2]);
                    p2 = Convert.ToDouble(parameters[3]);
                    p3 = Convert.ToDouble(parameters[4]);
                    outp = Efects.ColorMix(inp, (int)controllval, (int)p2, (int)p3, host.Mtrx_mem.StorageType);
                    break;
            }

            result.matrixtype = outp;
            return result;
        }

        public string Name
        {
            get { return "MTRX_COLOR"; }
        }

        public string Help
        {
            get { return Properties.Resources.man; }
        }

        public IPluginHost Host
        {
            get
            {
                return host;
            }
            set
            {
                host = value;
                host.Register(this);
            }
        }
    }
}
